package fr.cie.blog.modeldao.sql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import fr.cie.blog.beans.Article;
import fr.cie.blog.beans.Category;
import fr.cie.blog.domainservice.IServiceCategory;
import fr.cie.blog.modeldao.services.GestionBaseDeDonnees;
import fr.cie.blog.modeldao.services.Mapper;

/**
 * @author AetM
 * 
 */
public class CategoryDaoImplSql extends ModelDAO implements IServiceCategory {
	/**
	 * mapper.
	 */
	private Mapper mapper = new Mapper();

	/**
	 * Log4J the logger.
	 */
	private static final Logger LOG = Logger
			.getLogger(CategoryDaoImplSql.class);

	// Sql Properties

	/**
	 * connection.
	 */
	private Connection connection = null;

	/**
	 * result.
	 */
	private ResultSet result = null;

	/**
	 * category.
	 */
	private Category category;

	/**
	 * categoryList.
	 */
	private List<Category> categoryList;

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#save(java.lang.Object)
	 */
	@Override
	public final int save(final Category pCategory) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#delete(java.lang.Object)
	 */
	@Override
	public final int delete(final Category pCategory) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findAll()
	 */
	@Override
	public final List<Category> findAll() {
		// Variables
		PreparedStatement requete = null;
		Category pCategory = null;

		String requeteString = null;

		try {
			// Ouverture d'une connexion
			connection = super.getConnection();

			requeteString = "Select * FROM category ORDER BY category.name";

			requete = connection.prepareStatement(requeteString);

			// Execution de la requête
			result = requete.executeQuery();

			categoryList = new ArrayList<Category>();
			// On stocke le resultat dans une liste
			if (result != null) {
				while (result.next()) {

					pCategory = mapper.mapperCategory(result);

					categoryList.add(pCategory);

				}

			}
		} catch (final Exception e) {
			LOG.error("ERROR fonction get5lastArticles" + e);
		} finally {
			try {

				if (result != null) {
					GestionBaseDeDonnees.getInstance().closeResulset(result);
				}
				if (requete != null) {
					GestionBaseDeDonnees.getInstance().closeRequest(requete);
				}
				if (connection != null) {
					GestionBaseDeDonnees.getInstance().closeConnection(
							connection);
				}
			} catch (final Exception ex) {
				LOG.error("Erreur lors de la fermeture de la connexion avec "
						+ "la base de données dans la fonctionget5lastArticles"
						+ ex);
			}
		}

		LOG.info("Exit get5lastArticles  with articleList.size : "
				+ categoryList.size());

		return categoryList;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.cie.blog.domainservice.IServiceCategory#findByArticle(fr.cie.blog.
	 * beans.Article)
	 */
	@Override
	public final Category findByArticle(final Article pArticle) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findById(int)
	 */
	@Override
	public final Category findById(final long pIdentifier) {
		LOG.info("Entering findById(final int categoryId) with : "
				+ pIdentifier);
		// Variables
		PreparedStatement requete = null;
		String requeteString = null;

		try {
			// Ouverture d'une connexion
			connection = super.getConnection();

			requeteString = "Select * FROM category WHERE category.id = ?";

			requete = connection.prepareStatement(requeteString);

			requete.setLong(1, pIdentifier);

			// Execution de la requête
			result = requete.executeQuery();

			// On stocke le resultat dans une liste
			if (result != null && result.first()) {
				// On effectue le mapping des attributs avec les champs de
				// la table SQL
				category = mapper.mapperCategory(result);
			}

		} catch (final Exception e) {
			LOG.debug("Erreur dans la requete dans la "
					+ "classe CategoryDaoImplSql fonction findById()" + e);
		} finally {
			try {
				// Fermeture de la connexion
				if (result != null) {
					GestionBaseDeDonnees.getInstance().closeResulset(result);
				}
				if (requete != null) {
					GestionBaseDeDonnees.getInstance().closeRequest(requete);
				}
				if (connection != null) {
					GestionBaseDeDonnees.getInstance().closeConnection(
							connection);
				}
			} catch (final Exception ex) {
				LOG.debug("Erreur lors de la fermeture de la connexion avec "
						+ "la base de données dans la classe "
						+ "CategoryDaoImplSql fonction findById" + ex);
			}
		}

		LOG.info("Exit findById(categoryId) " + "with : " + category.getId());
		return category;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#update(java.lang.Object)
	 */
	@Override
	public final int update(final Category pCategory) {
		// TODO Auto-generated method stub
		return 0;
	}

}